package org.example.Handler;

import com.alibaba.fastjson2.JSON;
import org.example.core.Service.ComfyuiMessageService;
import org.example.core.pojo.MessageBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class ComfyuiMessageHandler extends TextWebSocketHandler {
    @Autowired
    ComfyuiMessageService comfyuiMessageService;
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("==============连接成功!");
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        MessageBase messageBase = JSON.parseObject(payload, MessageBase.class);
        comfyuiMessageService.handleMessage(messageBase);
        System.out.println("收到消息："+JSON.toJSONString(message));
    }
}
